#include "mainwindow.h"
#include "ui_mainwindow.h"

void MainWindow::drownHline(int n,QFrame **frame,int x,int y,int weight,int height,int distance)
{
    for(int i=0;i<n;i++)
    {
        frame[i]=new QFrame(ui->centralWidget);
        frame[i]->setGeometry(x,y+i*distance,weight,height);
        frame[i]->setFrameShape(QFrame::HLine);
    }
}

void MainWindow::drow4CheckBox(QCheckBox *checkbox[4],int x,int y,int weight,int height,int distance)
{
    for(int i=0;i<4;i++)
    {
        checkbox[i]=new QCheckBox;
        checkbox[i]->setParent(ui->centralWidget);
        checkbox[i]->setGeometry(x,y+i*distance,weight,height);
    }
}
void MainWindow::drowNCheckBox(int n,QCheckBox **checkbox,int x,int y,int weight,int height,int distance, int delta)
{
    for(int i=0;i<n;i++)
    {
        if(i!=0&&delta!=0&&i%4==0){
            y+=delta;
        }
        checkbox[i]=new QCheckBox;
        checkbox[i]->setParent(ui->centralWidget);
        checkbox[i]->setGeometry(x,y,weight,height);
        y+=distance;

    }
}

void MainWindow::drow4Word(QLabel *label[4],int x,int y,int weight,int height,int distance)
{
    for(int i=0;i<4;i++)
    {
        label[i]=new QLabel;
        label[i]->setParent(ui->centralWidget);
        label[i]->setGeometry(x,y+i*distance,weight,height);
        label[i]->setNum(i+1);
    }
}

void MainWindow::drow3Image(QLabel *label[3],int x,int y,int weight,int height,int distance)
{
    for(int i=0;i<3;i++)
    {
        label[i]=new QLabel;
        label[i]->setParent(ui->centralWidget);
        label[i]->setGeometry(x,y+i*distance,weight,height);
        label[i]->setPixmap(QPixmap::fromImage(*im));
    }
}

void MainWindow::changeBoxLineColor(int i){
  
    if(box[i]->checkState()==Qt::Checked){
        f[i]->setStyleSheet("color: green");
        f1[i]->setStyleSheet("color: green");
    }
    else{
        f[i]->setStyleSheet("color: black");
        f1[i]->setStyleSheet("color: black");

    }
}

void MainWindow::changeBox1LineColor(int i){
    if(i<4){
        box1Flag=!box1Flag;
        if(box1Flag==true){
            f_A[0]->setStyleSheet("color: green");
            fHConl[0]->setStyleSheet("color: green");
            fVConl[0]->setStyleSheet("color: green");
        }
        else{
            f_A[0]->setStyleSheet("color: black");
            fHConl[0]->setStyleSheet("color: black");
            fVConl[0]->setStyleSheet("color: black");
        }
    }
    if(i<8&&i>=4){
        box2Flag=!box2Flag;
        if(box2Flag==true){
            f_A[1]->setStyleSheet("color: green");
            fHConl[1]->setStyleSheet("color: green");
            fVConl[1]->setStyleSheet("color: green");
        }
        else{
            f_A[1]->setStyleSheet("color: black");
            fHConl[1]->setStyleSheet("color: black");
            fVConl[1]->setStyleSheet("color: black");
        }

    }
    if(i>=8){
        box3Flag=!box3Flag;
        if(box3Flag==true){
            f_A[2]->setStyleSheet("color: green");
            fHConl[2]->setStyleSheet("color: green");
            fVConl[2]->setStyleSheet("color: green");
        }
        else{
            f_A[2]->setStyleSheet("color: black");
            fHConl[2]->setStyleSheet("color: black");
            fVConl[2]->setStyleSheet("color: black");
        }

    }
}

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    box1Flag=false;
    box2Flag=false;
    box3Flag=false;
    drownHline(4,f,66,15,30,1,20);
    drow4CheckBox(box,52,10,15,14,20);
    //drowNCheckBox(12,box,52,10,15,14,20,20);

    
    boxMapper = new QSignalMapper;
    for(int i=0;i<4;i++){
        boxMapper->setMapping(box[i],i);
        connect(box[i],SIGNAL(stateChanged(int)),boxMapper,SLOT(map()));
    }
    connect(boxMapper,SIGNAL(mapped(int)),this,SLOT(changeBoxLineColor(int)));

    drowNCheckBox(12,boxm1,112,112,15,14,14,24);
    
    box1Mapper = new QSignalMapper;
    for(int i=0;i<12;i++){
        box1Mapper->setMapping(boxm1[i],i);
        connect(boxm1[i],SIGNAL(stateChanged(int)),box1Mapper,SLOT(map()));
    }
    connect(box1Mapper,SIGNAL(mapped(int)),this,SLOT(changeBox1LineColor(int)));

    //drow4CheckBox(boxm2,112,192,15,14,14);
    //drow4CheckBox(boxm3,112,272,15,14,14);
    drow4Word(linp,40,12,10,10,20);
    drownHline(4,fm1,104,118,10,1,14);
    drownHline(4,fm2,104,198,10,1,14);
    drownHline(4,fm3,104,278,10,1,14);

    drow4Word(lm1,104,108,10,10,14);
    drow4Word(lm2,104,188,10,10,14);
    drow4Word(lm3,104,268,10,10,14);

    drownHline(4,fme1,126,118,20,1,14);
    drownHline(4,fme2,126,198,20,1,14);
    drownHline(4,fme3,126,278,20,1,14);

    fbus[0]=new QFrame(ui->centralWidget);
    fbus[0]->setGeometry(96,15,8,306);
    fbus[0]->setLineWidth(8);
    fbus[0]->setFrameShape(QFrame::VLine);

    im=new QImage(":/new/prefix1/XOR1.png");
    drow3Image(lim,140,108,50,65,80);

    xM1=190;
    yM1=140;
    Hd=10;
    Vd=5;
    XConLine=244;
    YConLine=15;
    WConLine=150;
    HConLine=50;

    drownHline(4,f1,104,15,140,1,Vd);

    f_A[0]=new QFrame(ui->centralWidget);
    f_A[0]->setGeometry(xM1,yM1,20,1);
    f_A[0]->setFrameShape(QFrame::HLine);

    f_A[1]=new QFrame(ui->centralWidget);
    f_A[1]->setGeometry(xM1,yM1+80,20+Hd,1);
    f_A[1]->setFrameShape(QFrame::HLine);

    f_A[2]=new QFrame(ui->centralWidget);
    f_A[2]->setGeometry(xM1,yM1+160,20+2*Hd,1);
    f_A[2]->setFrameShape(QFrame::HLine);

    fVConl[0]=new QFrame(ui->centralWidget);
    fVConl[0]->setGeometry(xM1+20,15+4*Vd,1,yM1-15-4*Vd+1);
    fVConl[0]->setFrameShape(QFrame::VLine);

    fVConl[1]=new QFrame(ui->centralWidget);
    fVConl[1]->setGeometry(xM1+20+Hd,15+5*Vd,1,yM1-15-5*Vd+1+80);
    fVConl[1]->setFrameShape(QFrame::VLine);

    fVConl[2]=new QFrame(ui->centralWidget);
    fVConl[2]->setGeometry(xM1+20+2*Hd,15+6*Vd,1,yM1-15-6*Vd+1+160);
    fVConl[2]->setFrameShape(QFrame::VLine);

    fHConl[0]=new QFrame(ui->centralWidget);
    fHConl[0]->setGeometry(xM1+20,15+4*Vd,244-xM1-20,1);
    fHConl[0]->setFrameShape(QFrame::HLine);

    fHConl[1]=new QFrame(ui->centralWidget);
    fHConl[1]->setGeometry(xM1+20+Hd,15+5*Vd,244-xM1-20-Hd,1);
    fHConl[1]->setFrameShape(QFrame::HLine);

    fHConl[2]=new QFrame(ui->centralWidget);
    fHConl[2]->setGeometry(xM1+20+2*Hd,15+6*Vd,244-xM1-20-2*Hd,1);
    fHConl[2]->setFrameShape(QFrame::HLine);

    imConLine=new QImage(":/new/prefix1/ConnectionLine.png");
    LConLine=new QLabel;
    LConLine->setParent(ui->centralWidget);
    LConLine->setGeometry(XConLine,YConLine-8,WConLine,HConLine);
    LConLine->setPixmap(QPixmap::fromImage(*imConLine));
    
    drownHline(7,f_con,XConLine+WConLine,YConLine,20,1,5);

    fbus[1]=new QFrame(ui->centralWidget);
    fbus[1]->setGeometry(XConLine+WConLine+20,YConLine,8,306);
    fbus[1]->setLineWidth(8);
    fbus[1]->setFrameShape(QFrame::VLine);

    drownHline(4,f_B,XConLine+WConLine+28,YConLine,50,1,5);

}

MainWindow::~MainWindow()
{
    delete ui;
}
